package com.xh.car.service.impl;

import com.xh.car.bean.Kaptcha;
import com.xh.car.mapper.KaptchaMapper;
import com.xh.car.service.KaptchaService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

/**
 * @Author PangJunjie
 * @Date 2024/5/7/007
 */
@Service
@Transactional
public class KaptchaServiceImpl implements KaptchaService {
    @Autowired
    private KaptchaMapper kaptchaMapper;

    public void saveKaptcha(Kaptcha kaptcha) {
        kaptchaMapper.insertSelective(kaptcha);
    }

    public boolean checkKaptcha(Kaptcha kaptcha) {
        Kaptcha kt = kaptchaMapper.selectByPrimaryKey(kaptcha.getUuid());
        kaptchaMapper.deleteByPrimaryKey(kaptcha.getUuid());
        if (kt != null && kt.getCode().equalsIgnoreCase(kaptcha.getCode()) && kt.getExpire().getTime() > System.currentTimeMillis()) {

            return true;
        }
        return false;
    }

    public void clear() {
        kaptchaMapper.delete();
    }
}
